﻿using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
using MSDNWebcast.Commands;
using MSDNWebcast.Models;

namespace MSDNWebcast.ViewModels
{
    /// <summary>
    /// 主界面视图
    /// </summary>
    public class MainViewModel : ViewModelBase
    {
        #region Fields

        /// <summary>
        /// 退出
        /// </summary>
        private DelegateCommand m_ExitCommand;

        /// <summary>
        /// 页面信息
        /// </summary>
        private string m_PageInfomation;

        /// <summary>
        /// 工作区
        /// </summary>
        private ObservableCollection<WorkspaceViewModel> m_Workspaces;

        /// <summary>
        /// 课程视图
        /// </summary>
        private CoursesViewModel m_CoursesView;

        /// <summary>
        /// 下载视图
        /// </summary>
        private DownloadsViewModel m_Downloads;

        /// <summary>
        /// 视频播放
        /// </summary>
        private VideoPlayerViewModel m_VideoPlayer;

        /// <summary>
        /// 加载
        /// </summary>
        private LoadingViewModel m_Loading;

        #endregion

        #region Properties

        /// <summary>
        /// 工作区
        /// </summary>
        public ObservableCollection<WorkspaceViewModel> Workspaces
        {
            get
            {
                return m_Workspaces ?? (m_Workspaces = new ObservableCollection<WorkspaceViewModel>
                                                           {
                                                               CoursesView,
                                                               Downloads,
                                                               VideoPlayer
                                                           });
            }
        }

        /// <summary>
        /// 课程列表
        /// </summary>
        public CoursesViewModel CoursesView
        {
            get
            {
                if (m_CoursesView == null)
                {
                    m_CoursesView = new CoursesViewModel();
                    BindPropertyChange(m_CoursesView);
                }
                return m_CoursesView;
            }
        }

        /// <summary>
        /// 下载管理
        /// </summary>
        public DownloadsViewModel Downloads
        {
            get
            {
                if (m_Downloads == null)
                {
                    m_Downloads = new DownloadsViewModel();
                    BindPropertyChange(m_Downloads);
                }
                return m_Downloads;
            }
        }

        /// <summary>
        /// 视频播放
        /// </summary>
        public VideoPlayerViewModel VideoPlayer
        {
            get
            {
                if (m_VideoPlayer == null)
                {
                    m_VideoPlayer = new VideoPlayerViewModel();
                    BindPropertyChange(m_VideoPlayer);
                }
                return m_VideoPlayer;
            }
        }


        /// <summary>
        /// 加载视图
        /// </summary>
        public LoadingViewModel Loading
        {
            get
            {
                if (m_Loading == null)
                {
                    m_Loading = new LoadingViewModel();
                    m_Loading.PropertyChanged += m_Loading_PropertyChanged;
                }
                return m_Loading;
            }
        }

        /// <summary>
        /// 退出应用
        /// </summary>
        public ICommand ExitCommand
        {
            get { return m_ExitCommand ?? (m_ExitCommand = new DelegateCommand(Exit)); }
        }

        public bool ShowLoading { get { return !Loading.IsLoadingFinished; } }
        public bool ShowMainContent { get { return Loading.IsLoadingFinished; } }

        /// <summary>
        /// 页面信息
        /// </summary>
        public string PageInfomation
        {
            get { return m_PageInfomation; }
        }

        #endregion

        #region Constructor

        /// <summary>
        /// 
        /// </summary>
        public MainViewModel()
        {
            GlobalCommands.OpenVideo = new DelegateCommand<CourseFile>(OpenVideo, CanOpenVideo);
        }

        #endregion

        #region Methods

        /// <summary>
        /// 绑定工作区属性更新事件
        /// </summary>
        /// <param name="workspace"></param>
        private void BindPropertyChange(WorkspaceViewModel workspace)
        {
            workspace.PropertyChanged += delegate(object sender, PropertyChangedEventArgs e)
                                             {
                                                 if (e.PropertyName == "PageInfo" || e.PropertyName == "IsActive")
                                                     UpdatePageInfomation();
                                             };
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void m_Loading_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "IsLoadingFinished" && Loading.IsLoadingFinished)
            {
                OnPropertyChanged("ShowLoading");
                OnPropertyChanged("ShowMainContent");
                CoursesView.IsActive = true;
                CoursesView.InitialView();
            }
        }

        /// <summary>
        /// 打开视频
        /// </summary>
        public void OpenVideo(CourseFile courseFile)
        {
            this.VideoPlayer.IsActive = true;
            this.VideoPlayer.CourseFile = courseFile;
        }

        /// <summary>
        /// 能否打开课件
        /// </summary>
        /// <param name="courseFile"></param>
        /// <returns></returns>
        public bool CanOpenVideo(CourseFile courseFile)
        {
            return courseFile.CanPlay;
        }

        /// <summary>
        /// 更新页面文本信息
        /// </summary>
        private void UpdatePageInfomation()
        {
            foreach (var workSpace in Workspaces)
            {
                if (workSpace.IsActive && m_PageInfomation != workSpace.PageInfo)
                {
                    m_PageInfomation = workSpace.PageInfo;
                    OnPropertyChanged("PageInfomation");
                    break;
                }
            }
        }

        private void Exit()
        {
            Application.Current.Shutdown();
        }

        #endregion
    }
}
